package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TProdtask;
import com.wit.witmg.entity.TProdtech;
import com.wit.witmg.mapper.TProdtaskMapper;
import com.wit.witmg.service.ITProdtaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-01-07
 */
@Service
public class TProdtaskServiceImpl extends ServiceImpl<TProdtaskMapper, TProdtask> implements ITProdtaskService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TProdtask t= JSON.parseObject(json,TProdtask.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, Integer id, Integer stat, Integer oid, String pid,String bn,String ph) {
        if (id != null) {
            return JsonResult.success(getById(id));
        }

        QueryWrapper<TProdtask> qw = new QueryWrapper<>();
        qw.orderByDesc("id");

        if (pid != null) qw.eq("pid", pid);
        else if (stat != null) qw.eq("stat", stat);
        else if (oid != null) qw.eq("oid", oid);
        else if (bn != null) qw.like("bn", bn);
        else if (ph != null) qw.eq("ph", ph);
        else {
            return JsonResult.error(ErrorCode.ERR_200);
        }

        return JsonResult.success(list(qw));

    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
